"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by lei.chang on '23/04/2023'
    comment: 服务流程相关
"""
from sqlalchemy import func
from sqlalchemy.dialects.mysql import VARCHAR, INTEGER, DATETIME, TIMESTAMP, BOOLEAN

from project.core.base import CustomModel, CustomList
from project.core.base import Column
from project.model.basic import SopItem, InformConfig


class Flow(CustomModel):
    """ 服务流程表
    """
    __tablename__ = "flow"
    id = Column(INTEGER(unsigned=True), primary_key=True, autoincrement=True)
    code = Column(VARCHAR(30), nullable=False, server_default='', comment='服务流程编号')
    name = Column(VARCHAR(30), nullable=False, server_default='', comment='服务流程名称')
    introduction = Column(VARCHAR(200), nullable=False, server_default='', comment='服务流程介绍')
    hint = Column(VARCHAR(135), nullable=False, server_default='如有疑问，可以通过本机联系工作人员或房间内拨打前台电话，工作人员会为您服务。', comment='底部提示')
    figure = Column(VARCHAR(200), nullable=False, server_default='', comment='AI形象照(oss)')
    matching_intent = Column(VARCHAR(200), nullable=False, server_default='', comment='用户表达某些意愿匹配该服务流程')
    sop = Column(CustomList(2000), nullable=False, server_default='', comment='agent sop流程')
    # inform_type = Column(CustomList(100), nullable=False, server_default='', comment='通知类型：sms(短信)/tel(电话)')
    inform_config = Column(CustomList(2000), nullable=False, server_default='', comment='短信配置')
    third_party_url = Column(VARCHAR(200), nullable=False, server_default='', comment='第三方链接')
    organization_code = Column(VARCHAR(10), nullable=False, server_default='', comment='机构编号', is_displayed=False)
    is_template = Column(BOOLEAN, nullable=False, server_default='0', comment='是否模版 0: 否 1: 是')
    is_forbidden = Column(BOOLEAN, nullable=False, server_default='1', comment='是否被禁用: 0(启用)/1(禁用)')
    is_fixed = Column(BOOLEAN, nullable=False, server_default='0', comment='是否固定: 0(不固定)/1(固定)')
    is_deleted = Column(BOOLEAN, nullable=False, server_default='0', comment='是否删除 0: 未删除 1: 已删除', is_displayed=False)
    create_time = Column(DATETIME, nullable=False, server_default=func.now(), is_displayed=False)
    update_time = Column(TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now(), is_displayed=False)

    @property
    def inform_config_obj(self) -> list[InformConfig]:
        """ sop字段转对象
        :return:
        """
        inform_configs: list[InformConfig] = list()
        for v in self.inform_config or []:
            inform_config = InformConfig()
            inform_config.sets(v)
            inform_config.inform_type = v.get('inform_type', [])

            inform_configs.append(inform_config)

        return inform_configs


class FlowRole(CustomModel):
    """ 服务流程角色表
    """
    __tablename__ = "flow_role"
    id = Column(INTEGER(unsigned=True), primary_key=True, autoincrement=True)
    role_type = Column(VARCHAR(30), nullable=False, server_default='', comment='角色类型: user(用户)/worker(工作人员)')
    name = Column(VARCHAR(50), nullable=False, server_default='', comment='角色名称')
    phone = Column(VARCHAR(20), nullable=False, server_default='', comment='手机号')
    flow_id = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='服务流程id')
    flow_code = Column(VARCHAR(30), nullable=False, server_default='', comment='服务流程编号')
    remark = Column(VARCHAR(200), nullable=False, server_default='', comment='备注')
    is_deleted = Column(BOOLEAN, nullable=False, server_default='0', comment='是否删除 0: 未删除 1: 已删除', is_displayed=False)
    create_time = Column(DATETIME, nullable=False, server_default=func.now(), is_displayed=False)
    update_time = Column(TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now(), is_displayed=False)


class FlowPage(CustomModel):
    """ 服务流程自定义页面
    """
    __tablename__ = "flow_page"
    id = Column(INTEGER(unsigned=True), primary_key=True, autoincrement=True)
    page_type = Column(VARCHAR(30), nullable=False, server_default='', comment='页面类型: common(普通)/work_order(工单)')
    flow_id = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='服务流程id')
    flow_code = Column(VARCHAR(30), nullable=False, server_default='', comment='服务流程编号')
    flow_role_id = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='服务流程角色id')
    flow_process_id = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='服务流程状态id')
    component_id = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='组件id')
    title = Column(VARCHAR(30), nullable=False, server_default='', comment='组件标题信息')
    content = Column(VARCHAR(200), nullable=False, server_default='', comment='组件文案信息')
    remark = Column(VARCHAR(200), nullable=False, server_default='', comment='组件备注信息')
    rate = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='组件比例(%)')
    style = Column(VARCHAR(200), nullable=False, server_default='', comment='组件样式')
    items = Column(CustomList, nullable=False, server_default='', comment='组件显示的值列表')
    to_flow_process_id = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='服务流程状态id')
    rank = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='页面组件排序')
    is_deleted = Column(BOOLEAN, nullable=False, server_default='0', comment='是否删除 0: 未删除 1: 已删除', is_displayed=False)
    create_time = Column(DATETIME, nullable=False, server_default=func.now(), is_displayed=False)
    update_time = Column(TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now(), is_displayed=False)


class FlowProcess(CustomModel):
    """ 服务流程状态表
    """
    __tablename__ = "flow_process"
    id = Column(INTEGER(unsigned=True), primary_key=True, autoincrement=True)
    name = Column(VARCHAR(50), nullable=False, server_default='', comment='状态名称')
    flow_id = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='服务流程id')
    flow_code = Column(VARCHAR(30), nullable=False, server_default='', comment='服务流程编号')
    rank = Column(INTEGER, nullable=False, server_default='0', comment='流程顺序')
    remark = Column(VARCHAR(200), nullable=False, server_default='', comment='备注')
    digital_human_broadcast = Column(VARCHAR(100), nullable=False, server_default='', comment='数字人播报')
    is_display = Column(BOOLEAN, nullable=False, server_default='0', comment='是否显示 0: 隐藏 1: 显示')
    is_deleted = Column(BOOLEAN, nullable=False, server_default='0', comment='是否删除 0: 未删除 1: 已删除', is_displayed=False)
    create_time = Column(DATETIME, nullable=False, server_default=func.now(), is_displayed=False)
    update_time = Column(TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now(), is_displayed=False)
